package com.bootdo.common.utils.excel;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.StringUtils;

/**
 * Created by HP on 2019/4/12.
 */
public class OrderStatisticsExcelUtils2 {

    /**
     * 将用户的信息导入到excel文件中去
     * @param orderList 代发货订单列表
     * @param out 输出表
     */
    public static int exportUserExcel(List<Map<String,Object>> userList,Map<String, Object> map,ServletOutputStream out)
    {
        try{
            //1.创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //1.1创建合并单元格对象
            CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,10);//起始行,结束行,起始列,结束列
            //1.2头标题样式
            HSSFCellStyle headStyle = createCellStyle(workbook,(short)16);
            //1.3列标题样式
            HSSFCellStyle colStyle = createCellStyle(workbook,(short)13);
            //1.4 内容样式
            /*HSSFCellStyle cellStyle=workbook.createCellStyle();
            cellStyle.setWrapText(true); //自动换行 */
            //2.创建工作表
            HSSFSheet sheet = workbook.createSheet("统计信息列表");
            //2.1加载合并单元格对象
            sheet.addMergedRegion(callRangeAddress);
            //设置默认列宽
            sheet.setDefaultColumnWidth(20);
            //设置列宽
           /* sheet.setColumnWidth(3, 10000);
            sheet.setColumnWidth(4, 10000);
            sheet.setColumnWidth(7, 9000);
            sheet.setColumnWidth(8, 10000);*/
            /*sheet.setColumnWidth(0, 17);
            sheet.setColumnWidth(1, 10);
            sheet.setColumnWidth(2, 20);
            sheet.setColumnWidth(3, 23);
            sheet.setColumnWidth(4, 20);
            sheet.setColumnWidth(5, 5);
            sheet.setColumnWidth(6, 7);
            sheet.setColumnWidth(7, 17);
            sheet.setColumnWidth(8, 25);
            sheet.setColumnWidth(9, 15);
            sheet.setColumnWidth(10, 20);*/


            //3.创建行
            //3.1创建头标题行;并且设置头标题
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);

            //加载单元格样式
            /*map.put("orderSn",orderSn);
			map.put("eTime",eTime);
			map.put("sTime", sTime);
			map.put("export", export);
			map.put("status",status);
			map.put("flag",flag);*/
			String orderSn ="";
			String eTime ="";
			String sTime ="";
			String export ="";
			String status ="";
			String flag ="";
			if(map.get("orderSn")!=null&&!"".equals(map.get("orderSn").toString())){
				orderSn=map.get("orderSn").toString();
			}
			if(map.get("eTime")!=null&&!"".equals(map.get("eTime").toString())){
				eTime=map.get("eTime").toString();
			}else{
				eTime = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
			}
			if(map.get("sTime")!=null&&!"".equals(map.get("sTime").toString())){
				sTime=map.get("sTime").toString();
			}
			if(map.get("export")!=null&&!"".equals(map.get("export").toString())){
				if(map.get("export").toString().equals("0")){
					export="未导出";
				}else{
					export="已导出";
				}
			}
			if(map.get("status")!=null&&!"".equals(map.get("status").toString())){
				orderSn=map.get("status").toString();
				switch (orderSn) {
				case "1":
					orderSn="待支付";
					break;
				case "11":
					orderSn="补货";
					break;
				case "3":
					orderSn="待发货";
					break;
				case "4":
					orderSn="待收货";
					break;
				case "5":
					orderSn="退货中";
					break;
				case "6":
					orderSn="退货完成";
					break;
				case "7":
					orderSn="已完成";
					break;
				case "8":
					orderSn="取消订单";
					break;
				case "2":
					orderSn="秒杀订单";
					break;

				default:
					break;
				}
			}
			if(map.get("flag")!=null&&!"".equals(map.get("flag").toString())){
				flag=map.get("flag").toString();
				if(flag.equals("1")){
					flag="未使用优惠券";
				}else{
					flag="使用优惠券";
				}
			}
			
            cell.setCellStyle(headStyle);
            cell.setCellValue(sTime+"--"+eTime+" "+flag+status+export+orderSn+" 统计信息列表");

            //3.2创建列标题;并且设置列标题
            HSSFRow row2 = sheet.createRow(1);
            //String[] titles = {"订单号","待发货","金额","商品名","货品规格","SKU","数量","收件人姓名","收件人电话","收件人地址","买家留言"};
            String[] titles = {"商品名字","规格","数量"};
            for(int i=0;i<titles.length;i++)
            {
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(colStyle);
                cell2.setCellValue(titles[i]);
            }
            //4.操作单元格;将用户列表写入excel
            if(userList != null)
            {
                int size = 1;
                int j = 0;
                for(Map<String,Object> goodsMap : userList) {
                   // List<Map<String,Object>> goodsList = (List<Map<String,Object>>)(map.get("goodsList"));
                   // size = goodsList.size();
                    //创建数据行,前面有两行,头标题行和列标题行
                    HSSFRow row3 = sheet.createRow(j+2);
                    HSSFCell cell0 = row3.createCell(0);
                    cell0.setCellValue(StringUtils.getString(goodsMap.get("goodName")));
                    HSSFCell cell1 = row3.createCell(1);
                    cell1.setCellValue(StringUtils.getString(goodsMap.get("spec")));
                    HSSFCell cell2 = row3.createCell(2);
                    cell2.setCellValue(StringUtils.getString(goodsMap.get("number")));
                    j = j+size;
                }
            }
            //5.输出
            workbook.write(out);
            workbook.close();
            return 1;
            //out.close();
        }catch(Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     *
     * @param workbook
     * @param fontsize
     * @return 单元格样式
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize) {
        // TODO Auto-generated method stub
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        //创建字体
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints(fontsize);
        //加载字体
        style.setFont(font);
        return style;
    }




}
